import java.awt.*;

/**
 * Holds information about the player's ship and handles firing of projectiles
 * 
 * @author              James Wilkinson <jhwilkin@purdue.edu>
 * @version             1.7
 * @since               2011-11-20
 **/
public class Ship
{
  
    public Point loc;
    public Point tryLoc;
    public int lives;
    public boolean firing;
    private Point myProjectiles[];
    
    /** Time (ms) left for the ship to be invulnerable **/
    public int invulnerableTime;
    /** Time (ms) left for the ship to be invulnerable ?!?!?!?!?!?!?**/
    private GameSounds myGameSounds;
    
    /**
     * @param inGameSounds  Used to generate sound when ship fires
     * @param inProjectiles Used to create new projectiles
     **/
    public Ship(GameSounds inGameSounds, Point inProjectiles[])
    {
      
      //Initialize myProjectiles and myGameSounds with the
      //passed parameters.
      myGameSounds = inGameSounds;
      myProjectiles = inProjectiles;
      
      //Set the loc of the ship to (Settings.shipStartLoc.x, Settings.shipStartLoc.y)
      loc = new Point(Settings.shipStartLoc.x, Settings.shipStartLoc.y);
        
      //Set tryLoc to the same location as the ship's starting position.
      tryLoc = new Point(Settings.shipStartLoc.x, Settings.shipStartLoc.y);
      
      //Set the ship's lives to Settings.startLives
      lives = Settings.startLives;
        
      //Initialize invulnerableTime to 0.
      invulnerableTime = 0;
    }
    
    /**
     * Try to fire a projectile from the ship
     * 
     * Fires a projectile from the ship if the ship is not invulnerable and there are less than Settings.maxProjectiles projectiles.
     **/
    public void fire()
    {
      
      //Write code to implement the description above.
      //Make sure to play the appropriate sound effect if the ship fires.
      if(invulnerableTime != 0){
        return;
      }
      
      int num = Settings.maxProjectiles;
      if(Settings.superLaser){
        num = myProjectiles.length;
      }
      
      for(int i = 0; i < num; i++){
        if(myProjectiles[i] == null){
          
          myProjectiles[i] = new Point(loc.x + Settings.scale / 2 - 1, loc.y - 5);
          
          //Play Sound
          if(Settings.superLaser){
            myGameSounds.laser();
          }else{
            myGameSounds.cannon();
          }
          
          return;
        }
      }
    }
}
